<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>

<html>
<head>
    <s:set name="isNew" value="id == null"/>
    <s:set var="title">
        <s:if test="#isNew">创建</s:if><s:else>修改</s:else>角色
    </s:set>

    <title><s:property value="#title"/></title>
    <script type="text/javascript" src="${ctx}/js/jstree/jquery.jstree.js"></script>
    <script type="text/javascript" src="${ctx}/js/groupTree.js"></script>
    <script>
        $(document).ready(function() {
            //聚焦第一个输入框
            $("#name").focus();
            //为inputForm注册validate函数
            $("#inputForm").validate({
                                         rules:{
                                             name: "required"
                                         }
                                     });

            $('#submitButton').click(function() {
                $("#inputForm").submit();
            })
            
            initAuthorTree(); // add by sevenSpend 2011/11/30
        });
        // add by sevenSpend 2011/11/30 -->
        function initAuthorTree() {

            var updateURL = "/security/authority!updateDept.action";
            var authorTree = initGroupTreeCheckbox(${authorJson}, '${ctx}', updateURL, 'treeAuthor');

            var checkedAuthIds = $("#checkedAuthIdsStr").val().split(",");

            authorTree.bind("loaded.jstree", function (event, data) {
            	$("#treeAuthor").jstree("open_all");
				$("#treeAuthor").find("li").each(function() {
					for (var i = 0; i < checkedAuthIds.length; i++) {
						if ($(this).attr("id") == checkedAuthIds[i]) {
							$("#treeAuthor").jstree("check_node", $(this));
							break;
						}
					}
				});
            });
        }
        
        function getCheckList(obj) {
			var ids = getCheckId(obj);
			var strIds = "";
			for (var i=0; i<ids.length; i++) {
				strIds += ids[i];
				strIds += ",";
			}

			if (strIds == "") {
				$("#checkedAuthIdsStr").val("");
				return;
			}
			
			$("#checkedAuthIdsStr").val(strIds.substr(0, strIds.length-1));
		}

        function getCheckId(obj) {
			var ids =[];
			$.jstree._reference($("#treeAuthor")).get_checked(obj).each(function(i, n) {
				ids.push(n.id);
				if ($(this).find("> ul").length > 0) {
					ids.push(getCheckId($(this)));
				}
			});
			return ids;
		}
        // add by sevenSpend 2011/11/30 <--
    </script>
    <meta name="menu" content="roleManage"/>
</head>

<body>


<div class="content_block">
    <h2 class="jquery_tab_title"><s:property value="#title"/></h2>

    <form id="inputForm" action="role!save.action" method="post">
        <input type="hidden" name="id" value="${id}"/>
        <input type="hidden" name="revision" value="${revision}"/>
        <input type="hidden" name="orginDeptId" value="${deptId}"/>

        <p>
            <label for="name">角色名:</label>
            <input type="text" id="name" name="name" value="${name}" class="input-small"/>
        </p>

        <p>
        	<!-- update by sevenSpend 2011/11/30 start-->
        	<label>权限:</label>
            <div id="treeAuthor" style="width: 400px"></div>
        	<input type="hidden" id="checkedAuthIdsStr" name="checkedAuthIdsStr" value="${checkedAuthIdsStr}">
            <!-- update by sevenSpend 2011/11/30 end -->
        </p>

        <p>
        		<!-- update by sevenSpend 2011/11/30 start-->
                <!-- <a class="button" href="#" id="submitButton"><span>提交</span></a>-->
                <a class="button" href="#" id="submitButton" onclick="javascript:getCheckList()"><span>提交</span></a>
                <!-- update by sevenSpend 2011/11/30 end -->
            <a class="button" onclick="javascript:window.location.href='role.action'"><span>返回</span></a>
        </p>
    </form>

</div>
</body>
</html>
